package com.gloabalroam.testleak.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Scroller;

import com.gloabalroam.testleak.R;


public class ViewPagerIndicator extends View implements ViewPager.OnPageChangeListener {

    /**
     * 模块数量
     * */
    private int moduleCount;

    /**
     * 滑块在每一个模块中的间隔
     * */
    private float margin;

    /**
     * 滑块宽度
     * */
    private float sliderWidth;


    /**
     * 滑块位置
     * */
    private int sliderX;
    private int startX;

    /**
     * 每次划过的最大间隔
     * */
    private float slideMargin;

    private int sliderHeight;
    private Paint paint;
    private int viewHeight;

    private int currentPosition;
    private Scroller mScroller;


    public ViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);

        initFields(context,attrs);
        initView();
    }

    private void initFields(Context context,AttributeSet attrs)
    {
        TypedArray types = context.obtainStyledAttributes(attrs, R.styleable.ViewPagerIndicator);
        moduleCount = types.getInteger(R.styleable.ViewPagerIndicator_module_count,1);
        int sliderColor = types.getInteger(R.styleable.ViewPagerIndicator_slider_color, Color.WHITE);
        margin = types.getDimension(R.styleable.ViewPagerIndicator_margin_left_and_right,0);
        types.recycle();

        startX = (int) margin;
        currentPosition = 0;

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(sliderColor);
    }

    private void initView()
    {
        //计算滑块的宽度
        final ViewTreeObserver observer = getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (observer.isAlive()) observer.removeOnPreDrawListener(this);
                if (sliderWidth != 0) return true;
                int viewWidth = getWidth();
                sliderWidth = (viewWidth - moduleCount * 2 * margin) / moduleCount;
                slideMargin = margin * 2 + sliderWidth;

                viewHeight = getHeight();
                if (viewHeight > 2)
                {
                    sliderHeight = viewHeight - 2;
                }
                else
                {
                    sliderHeight = viewHeight;
                }
                paint.setStrokeWidth(sliderHeight);
                return true;
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(getResources().getDisplayMetrics().widthPixels,widthMeasureSpec),getDefaultSize(10,heightMeasureSpec));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawRect(sliderX,sliderHeight == viewHeight ? 0 : 1,sliderWidth + sliderX,sliderHeight == viewHeight ? viewHeight : (viewHeight - 1),paint);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        //如果当前位置和position相同，表示正在向右滑动
        if (position == currentPosition)
        {
            sliderX = startX + (int)(slideMargin * positionOffset);
        }
        //如果当前位置＜position，表示正在向右滑动并且是最后一次滑动
        else if (position > currentPosition)
        {
            startX += slideMargin;
            sliderX = startX;
            currentPosition = position;
        }
        //当前currentPosition大于position时，第一次向左移动
        else
        {
            startX -= slideMargin;
            sliderX = startX + (int)(slideMargin * positionOffset);

            currentPosition =position;
        }

        invalidate();
    }



    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }


}
